home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9333 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  92 lines

  1. Path: news.db.erau.edu!kochank
  2. From: kochank@news.db.erau.edu (Konrad Kochan)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help with a program PLS! :)
  5. Date: 1 Mar 1996 03:29:32 GMT
  6. Organization: Embry-Riddle Aeronautical University
  7. Message-ID: <4h5qut$5fp@deadbird.db.erau.edu>
  8. NNTP-Posting-Host: 155.31.1.1
  9. NNTP-Posting-User: kochank
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. I need help with this program:
  13.  
  14. Reads strings from file WORDS.IN, words in string are lower/upper caps
  15. sentences seperated by commas and periods (ie: Marry, had a little, lamb. DOH)
  16.  
  17. It has to sort the array and print it to file WORDS.OUT..
  18.  
  19. Ok, I got it ALMOST working.. it sorts everything accept that it puts
  20. CAPS words b4 lower case (Dupa before death, etc etc..)
  21.  
  22. Here is the code: (I hope)
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26.  
  27. /******* Function Declarations ********/
  28.    char dataArr[][26];
  29.    int index = 0;
  30.  
  31. /******** Main function of the program *********/
  32.  
  33. main()
  34. {
  35. int i;
  36.  FILE * fin, * fout;
  37.     if (!(fin = fopen("WORDS.IN", "r")))
  38.          {
  39.         printf("Something is wrong: Probably file missing.\n");
  40.         exit(1);   /* Program exits here if file is missing. */
  41.           }
  42.         printf("Input File = WORDS.IN\n");
  43.  
  44. /* The next function scans the file for all the strings, removes periods
  45.    and commas, and passes the "clean" string to dataArr
  46. */
  47.  
  48.         while ((fscanf(fin, "%s", & dataArr[index])) !=EOF)
  49.                 {
  50.                 strtok(dataArr[index], ",.");
  51.                 strtok(NULL, ",.");
  52.                 index++;
  53.                 }
  54.         close(fin);
  55.  
  56. /* Prints the original array, dataArr */
  57.  
  58.     for (i=0; i<index; i++)
  59.         printf("Original array: %s \n", dataArr[i]);
  60.  
  61. /* Sorts the array, dataArr */
  62.  
  63.     qsort(dataArr, index, 26*sizeof(char), strcmp);
  64.  
  65.  if (!(fout = fopen("WORDS.OUT", "w")))
  66.          {
  67.         printf("Something is wrong: File might be write protected.\n");
  68.         exit(1);   /* Program exits here if file can't be opened. */
  69.           }
  70.  
  71. /* Prints the sorted array to the screen */
  72.  
  73.     for (i=0; i<index; i++) {
  74.         printf("\tSorted array: %s\n", dataArr[i]);
  75.  
  76. /* Writes the array to the file, WORDS.OUT */
  77.  
  78.         fprintf(fout, "%s\n", & dataArr[i]);
  79.     }
  80. if (fclose(fout) == EOF)
  81.          {
  82.         printf("Something is wrong: File probably write protected.\n");
  83.         exit(1);
  84.          }
  85.  
  86. }
  87.  
  88. If you can help me fix this thing I will appreciate it! ..
  89.  
  90. Konrad
  91.  
  92.